import apis from '../../http/apis.js'

export default {
    namespaced: true,
    state: {
        // pages:0,
        // total:0,
        productsData: [],
        searchValue: '',
        // pageSize:4,
        // currentPage:1
    },
    mutations: {
        setProductsData(state, productsData) {
            state.productsData = productsData;
        },
        setSearchValue(state, searchValue) {
            state.searchValue = searchValue;
        }
    },
    actions: {
        // context 接收到的是当前仓库对象
        //获取保险产品数据
        async getProducts(context) {
            let { data } = await apis.products.getProducts({ searchValue: context.state.searchValue })
            if (data.code) {
                context.commit('setProductsData', data.data)
                this.products = data.data
            }

            //
            // this.products.products.forEach(item => {
            //   if(item.productsPic===''){
            //     item.productsPic='photo.png';
            //   }
            // });
        },
        //新增产品
        async addProducts(context, productsData) {
            let { data } = await apis.products.addProducts(productsData)
            if (data.code) {
                alert('添加成功');
                await context.dispatch('getProducts')
            }
        },

        //删除数据
        async deleteProducts(context, _id) {
            let { data } = await apis.products.deleteProducts({ _id });
            if (data.code) {
                alert('删除成功');
                await context.dispatch('getProducts')
            }
        }

    }
}